﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.Entities.ESGRatings;
using Sustainalytics.DataLayer.Shared;
using System.Collections.Concurrent;
using Sustainalytics.ESG.Entities;
using Sustainalytics.Utils;
using System.Web.Http.Dispatcher;
using System.Web.Http;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.ESGRatings.BusinessLogic.ESGOnlineReport;

namespace Sustainalytics.ReportGenerator.BL.Tests
{
    [TestClass]
    public class BulkDataReaderTests
    {
        private readonly IRepositoryCollection<ESGProfile> _repESGProfileCollection;
        private readonly IRepositoryCollection<ReferenceUniverse> _repRefUniverseCollection;

        public BulkDataReaderTests(IRepositoryCollection<ESGProfile> repESGProfileCollection, IRepositoryCollection<ReferenceUniverse> repRefUniverseCollection)
        {
            _repESGProfileCollection = repESGProfileCollection;
            _repRefUniverseCollection = repRefUniverseCollection;
        }

        [TestMethod]
        public void NextBulkTest()
        {
            var sw = new System.Diagnostics.Stopwatch();
            //  while (true)
            {
                {
                    //todo petre fix here 
                    ESGProfile defaultProfile = _repESGProfileCollection.ReadAllWhereFieldEqualsValue<string>("Name", "Default", null, 1).SingleOrDefault();


                    var refUniv = _repRefUniverseCollection.Read(defaultProfile.ReferenceUniverseId);
                    var expectids = refUniv.Companies.Select(x => x.CompanyId);

                    var bulksize = 100;

                    var config = new HttpConfiguration();

                    config.Filters.Add(new LogControllerFilter(true, true, false));

                    var container = PdfGeneration.Service.CompositionRoot.RegisterDependencies(config);

                    config.Services.Replace(typeof(IHttpControllerActivator), new SimpleInjectorControllerActivator(container));


                    ConcurrentBag<string> log = new ConcurrentBag<string>();
                    var bulkReader = new GeneratePdfBulkReader(
                        container.GetInstance<IRepositoryCollection<SubPeerGroupKeyEsgIssue>>(),
                        container.GetInstance<IRepositoryCollection<Analyst>>(),
                        container.GetInstance<IRepositoryCollection<IndicatorTemplate>>(),
                        container.GetInstance<IRepositoryCollection<CorporateData>>(),
                        container.GetInstance<IRepositoryCollection<FinancialData>>(),
                        container.GetInstance<IRepositoryCollection<RPCompanyProfile>>(),
                        container.GetInstance<IRepositoryCollection<RPCompanyESGComputationResult>>(),
                        container.GetInstance<IHistoricalDataBusinessLogicUnit>(),
                        container.GetInstance<IRepositoryCollection<RPIndicatorExtended>>(),
                        container.GetInstance<IRepositoryCollection<RPControversyIndicator>>(),
                        container.GetInstance<IRepositoryCollection<ComparisonGroupControversy>>(),
                        container.GetInstance<IRepositoryCollection<EventIndicator>>(),
                        container.GetInstance<IRepositoryCollection<Incident>>(),
                        container.GetInstance<IRepositoryCollection<CompanyKeyESGIssue>>(),
                        container.GetInstance<IRepositoryCollection<Source>>(),
                        bulksize, 
                        expectids, 
                        "mongodb://localhost:27017/ClientDW_A", 
                        defaultProfile, 
                        null, 
                        new System.Threading.CancellationToken());
                    sw.Start();

                    bulkReader.OneTimeDataRead();
                    var lastread = 0;
                    do
                    {
                        var data = bulkReader.NextESGRatingsBulk();
                        lastread = data.Count();
                    }
                    while (lastread > 0);
                    sw.Stop();

                    Console.WriteLine("bulk size:  {0}, duration {1}", bulksize, sw.ElapsedMilliseconds);
                    sw.Reset();

                }
            }
        }
    }
}
